# -*- coding: utf-8 -*-
#
# Copyright (C) 2011  Benjamin Heil
#
# This file is part of Feedmail.
#
# Feedmail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import datetime

from django.db import models
from django.contrib.auth.models import User

from feedmanager.parser import getFeedName

class FeedItem(models.Model):
    """Defines a single feed."""
    name = models.CharField(max_length=256)
    url = models.URLField()
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField(editable=False)
    feed_modified = models.CharField(max_length=32, null=True)
    etag = models.CharField(max_length=64, editable=False, null=True)
    is_gone = models.BooleanField(default=False, editable=False)

    def save(self, *args, **kwargs):
        if not self.id:
            self.name = getFeedName(self.url)
            self.created = datetime.datetime.now()
        self.modified = datetime.datetime.now()
        super(FeedItem, self).save(*args, **kwargs)

    def __unicode__(self):
        return u"Feed %s" % (self.name)

    class Meta:
        verbose_name = 'Feed'

class FeedAbo(models.Model):
    """Holds information about which user described which feed"""
    user = models.ForeignKey(User, related_name="abo_user")
    feed = models.ForeignKey(FeedItem)
    created = models.DateTimeField(editable=False)
    modified = models.DateTimeField(editable=False)

    def __unicode__(self):
        return u"%s: %s" % (self.user.username, self.feed.name)

    def save(self, *args, **kwargs):
        if not self.id:
            self.created = datetime.datetime.now()
        self.modified = datetime.datetime.now()
        super(FeedAbo, self).save(*args, **kwargs)

    class Meta:
        verbose_name = 'Abonnement'
        verbose_name_plural = 'Abonnement'

class FeedData(models.Model):
    fingerprint = models.TextField(max_length=4096, editable=False)
    fingerprint_md5 = models.CharField(max_length=32, editable=False)

    def __unicode__(self):
        return u"%s" % (self.fingerprint)

class FeedError(models.Model):
    feed = models.ForeignKey(FeedItem)
    timestamp = models.DateTimeField(editable=False)
    error_no = models.SmallIntegerField(editable=False)
    msg = models.CharField(max_length=256, editable=False)
    url = models.URLField(verify_exists=False)

    def __unicode__(self):
        return u"[%s] %s - %s (%s)" % (self.timestamp, self.error_no, self.msg, self.url)

    def save(self, *args, **kwargs):
        if not self.id:
            self.timestamp = datetime.datetime.now()
        super(FeedError, self).save(*args, **kwargs)

    class Meta:
        verbose_name = 'Feed-Fehler'
        verbose_name_plural = 'Feed-Fehler'
    